package bairui.international.weixin.portal.miniapp.controller.prize.nine;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;
import com.sh_lianjing.util.Util_UUID;

import bairui.international.model.domain.jpa.TB_PrizeItem;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.service.Service_PrizeItem;
import bairui.international.service.Service_User;
import bairui.international.service.Service_WinPrize;
import lombok.Getter;
import lombok.Setter;

/**
 *
 * 抽奖
 *
 * @generated
 * 
 * @see STest_C_Prize_Nine_PrizeRequest_Get 测试
 *
 */
@Controller
public class C_Prize_Nine_PrizeRequest_Get
 extends SC_Prize_Nine_PrizeRequest_Get 
{
	
	@Autowired private Service_PrizeItem service_PrizeItem;
	@Autowired private Service_WinPrize service_WinPrize;
	@Autowired private Service_User service_User;
	
	/**
	 * Get请求
	 */
	@Action(Get.class)
	@PostMapping(Get.RequestMappingValue)
	public @FomatOutput @ResponseBody Object get(
		ModelMap map, HttpServletRequest request,
		@RequestParam String skey,
		@RequestBody Input input
	) throws Exception {
		TB_User user = service_User.findOne(it->it.skey.eq(skey));
		List<TB_PrizeItem> list = service_PrizeItem.list(it->it.prize.id.eq(input.getPrizeId()));
		// 通过概率计算中奖
		Warp warp = winPrize(list);
		// 创建获奖
		TB_PrizeItem prizeItem = warp.getBean();
		String fileName = Util_UUID.getUUID();
		FileUtils.copyFile(
				new File(pathConfig.getPosition_Image_PrizeItem() + prizeItem.getFileUrl()), 
				new File(pathConfig.getPosition_Image_WinPrize() + fileName)
			);
		service_WinPrize.create(user, warp.getBean(), fileName);
		// 返回中奖id
		return new ItemOutput(warp.getBean());
	}
	
	/**
	 * 通过概率计算中奖
	 * @return 
	 */
	private static Warp winPrize(List<TB_PrizeItem> list){
		List<Warp> warps = new ArrayList<>();
		// 生产概率空间
		int i = 1;
		for(TB_PrizeItem item : list){
			int value = BigDecimal.valueOf(item.getRate()).multiply(BigDecimal.valueOf(10)).intValue();
			List<Integer> temp = new ArrayList<>();
			for(int j=0; j<value; j++){
				temp.add(i);
				i++;
			}
			warps.add(new Warp(item, temp));
		}
		// 生产随机数
		int resut = random(1, 1000);
		// 判断中奖
		for(Warp warp : warps){
//			System.out.println(warp.getBean().getId() + "====" + warp.getBean().getRate() + "===" + warp.getRange());
			if(warp.getRange().contains(resut)){
				return warp;
			}
		}
		throw new RuntimeException();
	}
	
	private static class Warp {
		private @Getter TB_PrizeItem bean;
		private @Getter List<Integer> range = new ArrayList<>();
		public Warp(TB_PrizeItem bean, List<Integer> temp){
			this.bean = bean;
			this.range = temp;
		}
	}
	
	/**
	 * 生成 [min, max] 的随机数
	 */
	public static int random(int min, int max) {
		Random random = new Random();
		return random.nextInt(max)%(max-min+1) + min;
    }
	
	
	private static class Input {
		@Getter @Setter private Long prizeId;
	}
	
	public static class ItemOutput {
		private @Getter long id;
		private @Getter String name;
		private @Getter String img;
		private @Getter Boolean noPrizeFlag;

		public ItemOutput(TB_PrizeItem it) {
			this.id = it.getId();
			this.name = it.getPrizeName();
			this.noPrizeFlag = it.getNoPrizeFlag();
			this.img = C_Prize_Nine_ItemShow_ShowImg.ShowImg.Operate(it.getId()).getUrl();
		}
		
	}

}
